<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-28 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();

		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			case 'search':
				$this->search();
				break;
			case 'do_search':
				$this->doSearch();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			case 'business_user':
				$this->businessUserList();
				break;
			default:
				$this->main();
				break;
		}

		$body = ob_get_clean();

		$this->showBody($body);
	}
	
	private function main()
	{
		$this->doSearch();
	}

	private function add()
	{
		$request = array();
		if(isset($this->request['c']))
		{
			$request = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$request['name'] = isset($request['name']) ? trim($request['name']) : '';
		$request['group_id'] = isset($request['group_id']) ? (int)$request['group_id'] : '';
		$request['phone'] = isset($request['phone']) ? trim($request['phone']) : '';
		$request['password'] = '';
		
		$group_list = get_user_group_list();
		
		$groups = array();
		foreach($group_list as $k=>$v)
		{
			$groups[] = array('name'=>$v['name'], 'value'=>$v['id']);
		}
		
		require_once ROOT_PATH.'include/function/form.func.php';
		$select = select('group_id', $groups, $request['group_id'], 'style="width:100px;"');
		
		$action_name = '添加用户';
		$action = "do_add";
		include template('admin/user_info');
	}
	
	private function doAdd()
	{
		$request['time'] = time();
		
		$request['name'] = isset($this->request['name']) ? trim($this->request['name']) : '';
		$request['password'] = isset($this->request['password']) ? trim($this->request['password']) : '';
		$request['group_id'] = isset($this->request['group_id']) ? trim($this->request['group_id']) : '';
		$request['phone'] = isset($this->request['phone']) ? trim($this->request['phone']) : '';
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "manage.php?m=user&a=add&c=$key";
		
		if(!$request['name'] || !$request['password'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		if(strlen($request['name']) > 29)
		{
			$this->messager("用户名超长", $redirect);
		}
		if(strlen($request['phone']) > 29)
		{
			$this->messager("电话超长", $redirect);
		}
		
		$request['password'] = $this->createPassword($request['time'], $request['password']);
		
		$result = add_user($request);
		
		if($result)
		{
			$this->messager("添加成功", 'manage.php?m=user&a=search');
		}
		else
		{
			$this->messager("添加失败", $redirect);
		}
	}
	
	private function doDelete()
	{		
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		if(!$ids)
		{
			$this->messager("请指定ID", 'manage.php?m=user');
		}
		
		$result = false;
		if(stripos($ids, ','))
		{
			$ids = explode(',', $ids);
			
			foreach($ids as $id)
			{
				if($id != $this->user['id'])
				{
					$result = delete_user($id);
				}
			}
		}
		else if($ids != $this->user['id'])
		{
			$result = delete_user($ids);
		}
				
		if($result)
		{
			$this->messager("删除成功", 'manage.php?m=user');
		}
		else
		{
			$this->messager("删除失败", 'manage.php?m=user');
		}
	}
	
	private function search()
	{
		$group_list = get_user_group_list();

		$role_count = count($group_list) + 1;
		
		$action = "do_search";
		include template('admin/user_search');
	}
	
	private function doSearch()
	{
		$page = $this->request['offset'] + 1;
				
		$group_id = isset($this->request['group_id']) ? $this->request['group_id'] : '';
		$username = isset($this->request['username']) ? trim($this->request['username']) : '';
		$phone = isset($this->request['phone']) ? trim($this->request['phone']) : '';
		
		if(check_array($group_id))
		{
			$group_id = implode(',', $group_id);
		}
		
		$query_link = "manage.php?m=user";
		$where_list = array();
				
		if($group_id)
		{
			$where_list['group'] = "u.user_group_id in ($group_id)";
			$query_link .= "&group_id=".urlencode($group_id);
		}
		
		if($username)
		{
			if(substr($username, 0, 1) == '*' || substr($username, -1) == '*')
			{
				$username = (substr($username, 0, 1) == '*') ? '%'.substr($username, 1) : $username;
				$username = (substr($username, -1) == '*') ? substr($username, 0, -1).'%' : $username;
				$where_list['username'] = " u.`name` like '$username'";
			}
			else
			{
				$where_list['username'] = " u.`name` = '$username'";
			}
			$query_link .= "&username=".urlencode($username);
		}
		
		if($phone)
		{
			$where_list['nickname'] = " u.`phone` = '$phone'";
			$query_link .= "&phone=".urlencode($phone);
		}
		
		$where = empty($where_list) ? '' : ' where '.implode(' and ', $where_list).' ';
		
		$sql = " select count(1) as `total_record` from `tra_user` u {$where} ";
		$total_record = DBFunction::getInstance()->getOne($sql);
		
		$item_list = array();
		if($total_record > 0)
		{
			$offset = $page * $this->request['offset'];
			$sql = "select u.*, g.`name` as group_name 
						from `tra_user` u 
						inner join `tra_user_group` g on g.`id` = u.`user_group_id`
						{$where} 
						order by u.`id` asc 
						limit {$offset},{$this->request['limit']} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
		}
		
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '20 50 100 200 500');
		
		$action = "do_delete";
		include template('admin/user_search_list');
	}
	
	private function modify()
	{
		$user_id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$user_id)
		{
			$this->messager("请指定ID", 'manage.php?m=user');
		}
		
		if(isset($this->request['c']) && ($request = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$request = get_user(array('id'=>$user_id));
			
			if(!isset($request['id']) || !$request['id'])
			{
				$this->messager("用户不存在", 'manage.php?m=user');
			}
		}
		
		$group_list = get_user_group_list();
		
		$groups = array();
		foreach($group_list as $k=>$v)
		{
			$groups[] = array('name'=>$v['name'], 'value'=>$v['id']);
		}
		
		require_once ROOT_PATH.'include/function/form.func.php';
		$select = select('group_id', $groups, $request['group_id'], 'style="width:100px;"');
		
		$action = 'do_modify&id='.$request['id'];
		$action_name = '编辑用户';
		include template('admin/user_info');
	}
	
	private function doModify()
	{
		$uid = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		$redirect = 'manage.php?m=user';
		
		if(!$uid)
		{
			$this->messager("请指定ID", $redirect);
		}
		
		$user = get_user(array('id'=>$uid));
		
		if(!isset($user['id']) || $user['id'] < 1)
		{
			$this->messager("用户不存在", $redirect);
		}
		
		//$info = $this->_check_business_info();
		$info['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		$info['phone'] = isset($this->request['phone']) ? $this->request['phone'] : '';
		$info['password'] = isset($this->request['password']) ? $this->request['password'] : '';
		$info['group_id'] = isset($this->request['group_id']) ? $this->request['group_id'] : '';
		
		if(!$info['name'])
        {
			$this->messager("用户名不能为空", 'manage.php?m=user&a=modify&id='.$uid);
		}
		if(strlen($info['name']) > 29)
		{
			$this->messager("用户名超长", 'manage.php?m=user&a=modify&id='.$uid);
		}
		if(strlen($info['phone']) > 29)
		{
			$this->messager("电话超长", 'manage.php?m=user&a=modify&id='.$uid);
		}
		
		$info['id'] = $uid;
		$info['uid'] = $this->user['id'];
		
		if(!$info['password'])
		{
			$info['password'] = $user['password'];
		}
		else
		{
			$info['password'] = $this->createPassword($user['create_date'], $info['password']);
		}
		
		$result = update_user($info);

		if($result)
		{
			$this->messager("修改成功", $redirect);
		}
		else
		{
			$this->messager("修改失败", $redirect);
		}
	}
	
	private function businessUserList()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;

		$name = isset($this->request['name']) ? trim($this->request['name']) : '';
		
		$query_link = "manage.php?m=user&a=business_user&name=".urlencode($name);
		
		$result = get_business_user_list(array('name'=>$name));
		$list = $result['result'];
		
		$types = array('bu'=>'餐饮', 'ho'=>'酒店', 'sc'=>'景区', 'sh'=>'免税店');
		$type_names = array('bu'=>'business', 'ho'=>'hostel', 'sc'=>'scenic', 'sh'=>'shop');
		
		if(check_array($list))
		{
			foreach($list as &$temp)
			{
				$temp['type_name'] = isset($type_names[$temp['type']]) ? $type_names[$temp['type']] : '';
				$temp['type'] = isset($types[$temp['type']]) ? $types[$temp['type']] : '';
			}
		}
		
		$page_html = page($result['count'], $this->request['limit'], $page, $query_link, '20 50 100 200 500');
		
		include template('admin/business_user_list');
	}
}